package edu.upf.opencities.dao.connection;

import org.apache.log4j.Logger;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;


public class TwitterConnectorImpl implements TwitterConnector {
    
	public static Logger logger = Logger.getLogger(TwitterConnectorImpl.class);


	@Override
	public void updateStatus(String consumerKey, String consumerSecret,
			String token, String tokenSecret,
			String message) {

		
		try {
			Twitter twitter = new TwitterFactory().getInstance();
			twitter.setOAuthConsumer(consumerKey, consumerSecret);

			AccessToken accessToken = new AccessToken(token, tokenSecret);
			twitter.setOAuthAccessToken(accessToken);

			Status status = twitter.updateStatus(message);
			logger.info("Update message from user <"+status.getUser()+"> sended: "+status.getText());
		} catch (TwitterException e1) {
			e1.printStackTrace();
			logger.info("Update message problem with connection or authentication");
		}


		
	}
	

}
